package com.ctsi.flowable.engine.config;

import com.ctsi.flowable.engine.service.FlowableService;
import com.ctsi.flowable.idm.remote.RemoteIdmIdentityServiceImpl;
import com.ctsi.framework.common.config.ApplicationAutoConfiguration;
import org.flowable.idm.spring.SpringIdmEngineConfiguration;
import org.flowable.spring.boot.FlowableProperties;
import org.flowable.spring.boot.idm.FlowableIdmProperties;
import org.flowable.spring.boot.idm.IdmEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @Auther: zhangjw
 * @Date: 2018/8/24 07:29
 * @Description:
 */
@Configuration
@AutoConfigureAfter(ApplicationAutoConfiguration.class)
public class FlowableIdmConfig extends IdmEngineAutoConfiguration {

    @Autowired
    private FlowableService flowableService;

    public FlowableIdmConfig(FlowableProperties flowableProperties, FlowableIdmProperties idmProperties) {
        super(flowableProperties, idmProperties);
    }

    @Override
    public SpringIdmEngineConfiguration idmEngineConfiguration(DataSource dataSource, PlatformTransactionManager platformTransactionManager) {
        SpringIdmEngineConfiguration conf= super.idmEngineConfiguration(dataSource, platformTransactionManager);
        // 弃用此方式
        //conf.setGroupEntityManager(new GroupEntityManagerImpl(organiseServiceProxy));
        //conf.setUserEntityManager(new UserEntityManagerImpl(userServiceProxy));

        // 采用此种方式覆盖默认的Idm引擎
        conf.setIdmIdentityService(new RemoteIdmIdentityServiceImpl(flowableService));

        /**
         *  不生成idm数据库表
         */
        conf.setUsingRelationalDatabase(false);
        return conf;
    }
}
